﻿@model Otter.Models.ArticleHistoryModel
@{
  ViewBag.Title = "History of " + Model.Title;
  int penultimateRevision = Model.HistoryRecords.Any() ? Model.HistoryRecords.Max(r => r.Revision) : 0;
}
<h1>@Model.Title</h1>
<div class="row">
  @using (@Html.BeginForm("Compare", "Article", new { id = Model.UrlTitle }, FormMethod.Get))
  {
    <div class="col-md-9">
      <table class="table table-condensed">
        <thead>
          <tr>
            <th>Version</th>
            <th>Updated by</th>
            <th>Updated when</th>
            <th>Comment</th>
            <th>View</th>
            <th>From</th>
            <th>To</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>@Model.Revision (current)</td>
            <td>@(string.IsNullOrEmpty(Model.UpdatedBy) ? string.Empty : Model.UpdatedByDisplayName ?? Model.UpdatedBy)</td>
            <td>@Model.UpdatedDtm.ToString("g", System.Globalization.CultureInfo.CurrentUICulture)</td>
            <td>@Model.Comment</td>
            <td>@Html.ActionLink("View", "Read", new { id = Model.UrlTitle })</td>
            <td>&nbsp;</td>
            <td>
              <input type="radio" name="compareTo" value="@Model.Revision" checked="checked"@{ if (!Model.HistoryRecords.Any())
                                                                                               { <text>disabled="disabled"</text> } }/></td>
          </tr>
          @for (int i = 0; i < Model.HistoryRecords.Count(); i++)
          {
            Otter.Models.ArticleHistoryRecord record = Model.HistoryRecords.ElementAt(i);
            <tr>
              <td>@record.Revision</td>
              <td>@(string.IsNullOrEmpty(record.UpdatedBy) ? string.Empty : record.UpdatedByDisplayName ?? record.UpdatedBy)</td>
              <td>@record.UpdatedDtm.ToString("g", System.Globalization.CultureInfo.CurrentUICulture)</td>
              <td>@record.Comment</td>
              <td>@Html.ActionLink("View", "Read", new { id = Model.UrlTitle, revision = record.Revision })</td>
              <td>
                <input type="radio" name="compareFrom" value="@record.Revision"@{ if (record.Revision == penultimateRevision)
                                                                                  { <text>checked="checked"</text> } } /></td>
              <td>
                @if (i < Model.HistoryRecords.Count() - 1)
                {
                  <input type="radio" name="compareTo" value="@record.Revision" />
                }
              </td>
            </tr>
          }
        </tbody>
      </table>
    </div>
    if (Model.HistoryRecords.Any())
    {
    <div class="col-md-3">
    <div class="compare" data-spy="affix" data-offset-top="100">
      <button type="submit" class="btn btn-primary">Compare versions</button>
    </div>
    </div>
    }
  }
</div>
@section scripts
{
  <script type="text/javascript">
    $(document).ready(function () {
      $("input:radio[name='compareTo']").change(function () {
        // If the "compareFrom" value is greater or equal to the new "compareTo" value, change it to the previous revision.
        var compareTo = parseInt($(this).val());
        var compareFrom = parseInt($("input:radio[name='compareFrom']:checked").val());
        if (compareFrom >= compareTo) {
          $("input:radio[name='compareFrom']").val([compareTo - 1]);
        }

        // Disable all "compareFrom" values greater or equal to the new "compareTo" value.
        $("input:radio[name='compareFrom']").each(function () {
          $(this).prop("disabled", parseInt($(this).attr("value")) >= compareTo);
        });
      });
    });
  </script>
}
